import { Joint_Live_List_Status } from '@/pluginPackage/liveModule/apis/CreateLive/model'
import storage from '@/utils/storage'
// 主播端
const viewLivePageUrl = '/pluginPackage/liveModule/views/liveAnchor'
// 用户端
const viewLivePageUserUrl = '/pluginPackage/liveModule/views/Audience'
// 本地存储 key
const CREATE_LIVE_ROOM_STORAGE_KEY = 'CREATE_LIVE_ROOM_STORAGE_KEY'
// 本地存储 key
const USER_LIVE_ROOM_STORAGE_KEY = 'CREATE_LIVE_ROOM_STORAGE_KEY'
enum TABBAR_URL {
  LIVE_ACTIVE = '',
  LIVE_UN_ACTIVE = '',
  GOODS_ACTIVE = '',
  GOODS_UN_ACTIVE = '',
  HOME_UN_ACTIVE = '',
  HOME_ACTIVE = '',
}
function toLive(data: { id: string; pushAddress: string }) {
  // @ts-ignore
  // eslint-disable-next-line no-undef
  getApp().globalData.groupID = data.id
  // 推流地址过长 为了安全考虑利用本地传输 immediately 立即开播
  storage.set(CREATE_LIVE_ROOM_STORAGE_KEY, { ...data, immediately: true })
  uni.redirectTo({ url: `${viewLivePageUrl}` })
}
function toUserLive(data: { id: string; pullAddress: string; status: Joint_Live_List_Status }) {
  // @ts-ignore
  // eslint-disable-next-line no-undef
  getApp().globalData.groupID = data.id
  // 推流地址过长 为了安全考虑利用本地传输 immediately 立即开播
  storage.set(USER_LIVE_ROOM_STORAGE_KEY, { ...data, immediately: true })
  uni.navigateTo({ url: `${viewLivePageUserUrl}` })
}
function getUserLiveStorageSyncFn() {
  return storage.get(USER_LIVE_ROOM_STORAGE_KEY) || {}
}

// pullAddress 是一个逗号字符串拼接的拉流地址 顺序为 RTMP地址  FLV地址  HLS地址  WebRTC地址
// video 组件微信小程序仅支持 HLS地址 APP使用 FLV地址
function formatPullAddress(pullAddress: string) {
  let index = 1
  // #ifdef APP-PLUS
  // #endif
  // #ifdef MP-WEIXIN
  index = 2
  // #endif
  return pullAddress.split(',')[index]
}
export {
  viewLivePageUrl,
  TABBAR_URL,
  CREATE_LIVE_ROOM_STORAGE_KEY,
  USER_LIVE_ROOM_STORAGE_KEY,
  viewLivePageUserUrl,
  toLive,
  toUserLive,
  getUserLiveStorageSyncFn,
  formatPullAddress,
}
